<?php
include '../include/functions.php';
//set session save path
session_save_path($session_path);
// Initialize a session. 
session_start();
$count++;
$start = time();
$sessionId = session_id();
if((!$userID) || ($PlayNHL == 0))
{
	header("Location: schedule.php"); /* Redirect browser */
	/* Make sure that code below does not get executed when we redirect. */
	exit;
}
//include 'include/SurvivalPicks.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//Dtd html 4.01 Transitional//EN"
"http://www.w3.org/tr/html4/loose.dtd">
<html>
  <head>
    <title>JoshHughes.com NHL Hockey Pool</title>
	<link rel="shortcut icon" href="http://hockey.joshhughes.com/NHL/images/nhlFavicon.gif" />
	<link rel="stylesheet" href="http://hockey.joshhughes.com/css/NHL.css" type="text/css">
    <script src="http://hockey.joshhughes.com/scripts/prototype.js" type="text/javascript"></script>
	<script src="http://hockey.joshhughes.com/scripts/scriptaculous.js" type="text/javascript"></script>
	<script src="http://hockey.joshhughes.com/scripts/hockey.js" type="text/javascript"></script>
  </head>
<body>
<div id="pagewidth" >
	<div id="header" >
	
		<?PHP include_once("layout/header.html"); ?>
		
	</div>
			<div id="wrapper" class="clearfix" > 
			<div id="maincol" >

			<!-- MainContents -->
				<h3>Series Picks </h3>
				<p>Pick who you think will win the best-of-seven game series. (5 points for each correct pick) <br/>
				Pick how many games you think the series will last.  (2 points for each correct pick)</p>
				<p>Make your pick by selecting the team from the drop-down list at the right, or by clicking on the team logo.  Click the "Submit/Update" button to submit the changes.</p>
				<table width="510" cellpadding="5" cellspacing="0">
					<tr>
					<td class="bluebg">
					<?php
					//Connect To Database
					dbConnect();
					
					//Write SQL Query to extract specified data
					$SQL = "SELECT * FROM NHLSeries ORDER BY Week ASC";
					//Open Record Set using SQL Query
					$result = mysql_query($SQL) or die("Cannot query the database to get NHL series data.<br/>");
					$numSeries = mysql_num_rows($result);
						
					?>
					<form method="POST" name="PicksForm" action="submitseriespicks.php" onSubmit="return checkPriority(this)">
					<table bgcolor=#FFFFFF callpadding="5" cellspacing="0" width="700" align="center" border="0">
					<tr><TH align="left">Date</th><td colspan="8" align="center"><?= date("l F jS, Y g:i A", time() + (3600 * 3))?> EST</td></tr>
					<tr>
						<th>Series</th>
						<th width="40">&nbsp;<!-- Team1 IMG Column --></th>
						<th width="100">Team 1</th>
						<th width="20">&nbsp;<!-- Vs. Column --></th>
						<th width="100">Team 2</th>
						<th width="40">&nbsp;<!-- Team2 IMG Column --></th>
						<th align="Left" width="100">Picks</th>
						<th align="center" width="20">Games Guess</th>
						<th width="150">Winner</th>
						<th width="30">&nbsp;</th>
						<th align="center" width="10"># Games</th>
					</tr>
					<?
					$CurrentDate = 20031022;
					$row = -1;
					$update = false;
					$gamesOver = true;
						
					do{
						if (($seriesRow["Team1"] != NULL) && ($seriesRow["Team1"] != ""))
						{
						
							$UserPickedSQL = "SELECT * FROM NHLSeriesPicks WHERE (UserID = ".$userID.") AND (Week = ".$seriesRow["Week"].")";
							$UserPickedresult = mysql_query($UserPickedSQL) or die("Cannot query the database to see if user has made picks.<br/>");
							$UserPickedrow = mysql_fetch_array($UserPickedresult);
							if ($UserPickedrow["Week"] != "") 
								$update = true;
							//increment i for the row color
							$row++;
							switch($seriesRow["Week"])
							{
								case 1:
									{
										echo "<tr bgcolor=\"".getRowColor($row)."\"><td colspan=\"11\" align=\"center\"><strong>--- ROUND ONE ---</strong></td></tr>";
										$row++;
									}
									break;
								case 9:
									{
										echo "<tr bgcolor=\"".getRowColor($row)."\"><td colspan=\"11\" align=\"center\"><strong>--- QUARTER FINALS ---</strong></td></tr>";
										$row++;
									}
									break;
								case 13:
									{
										echo "<tr bgcolor=\"".getRowColor($row)."\"><td colspan=\"11\" align=\"center\"><strong>--- SEMI FINALS ---</strong></td></tr>";
										$row++;
									}
									break;
								case 15:
									{
										echo "<tr bgcolor=\"".getRowColor($row)."\"><td colspan=\"11\" align=\"center\"><strong>--- STANLEY CUP FINALS ---</strong></td></tr>";
										$row++;
									}
									break;
							}
							echo "<tr bgcolor=\"".getRowColor($row)."\"><td>".$seriesRow["Week"]."</td>";
							echo "<td align=\"center\">
									<img src=\"http://hockey.joshhughes.com/NHL/images/small/".$seriesRow["Team1"].".gif\" title=\"Click to select ".$seriesRow["Team1"]."\" onClick=\"goSet(PicksForm.Series".$seriesRow["Week"].",'".$seriesRow["Team1"]."')\" />
									</td>
									<td align=\"left\">".$seriesRow["Team1"]."</td><td> Vs. </td>";
							echo "<td align=\"right\">".$seriesRow["Team2"]."</td>
							<td align=\"center\">
							<img src=\"http://hockey.joshhughes.com/NHL/images/small/".$seriesRow["Team2"].".gif\" title=\"Click to select ".$seriesRow["Team2"]."\" onClick=\"goSet(PicksForm.Series".$seriesRow["Week"].",'".$seriesRow["Team2"]."')\" />
							</td>";
						 
							if (($UserPickedrow["Team"] != "") && ($UserPickedrow["Team"] != NULL))
							{//user has made a pick for this series.
							
								if ($UserPickedrow["Team"] == $seriesRow["Winner"])
								{	//Series has ended and the user picked the correct winner.
									echo "<td><span style=\"color: #FF0000; font-weight: bold;\">".$seriesRow["Winner"]."</span></td>";
								
									if ($UserPickedrow["NumGames"] == $seriesRow["NumGames"])
										echo "<td align=\"center\"><span style=\"color: #FF0000; font-weight: bold;\">".$UserPickedrow["NumGames"]."</span>";
									else
										echo "<td align=\"center\">".$UserPickedrow["NumGames"]."</td>";
								}
								else
								{
									if ($seriesRow["Started"] == 1)
									{	// Series is in progress, or has ended.  User cannot make pick anymore.
										echo "<td>".$UserPickedrow["Team"]."</td>";
										if ($UserPickedrow["NumGames"] == $seriesRow["NumGames"])
											echo "<td align=\"center\"><span style=\"color: #FF0000; font-weight: bold;\">".$UserPickedrow["NumGames"]."</span>";
										else
											echo "<td align=\"center\">".$UserPickedrow["NumGames"]."</td>";
									}
									else
									{	//Series has Not started yet, so let the user change their pick.
										$gamesOver = false;
										echo "<td align=\"left\"><select name=\"Series".$seriesRow["Week"]."\" class=\"select\"";
										if ($seriesRow["Team1"] == "TBD")
											echo " disabled";
										echo ">";
										if($UserPickedrow["Team"] == $seriesRow["Team1"])
											echo "<option selected value=\"".$seriesRow["Team1"]."\">".$seriesRow["Team1"]."</option>";
										else
											echo "<option value=\"".$seriesRow["Team1"]."\">".$seriesRow["Team1"]."</option>";
							
										if($UserPickedrow["Team"] == $seriesRow["Team2"])
											echo "<option selected value=\"".$seriesRow["Team2"]."\">".$seriesRow["Team2"]."</option>";
										else
											echo "<option value=\"".$seriesRow["Team2"]."\">".$seriesRow["Team2"]."</option>";
							
										echo "</select></td><td align=\"center\">".makeDropDown($seriesRow["Week"],$seriesRow["Team1"],$UserPickedrow["NumGames"])."</td>";
									}
								}//end if ($UserPickedrow["Team"] == $seriesRow["Winner"])
								
							}
							else	//User has not picked, so give the user the options to pick from.
							{	
								if ($seriesRow["Started"] == 1)
									echo "<td>In Progress</td><td>&nbsp;</td>"; //The Series has started.  User cannot make pick anymore.
								else		//The series has not started yet, let user pick.
								{
									echo "<td align=\"left\"><select name=\"Series".$seriesRow["Week"]."\" class=\"select\" style=\"background-color: #FFFF99;\"";
									if ($seriesRow["Team1"] == "TBD")
											echo " disabled";
									echo "><option selected value=\"\">Make Pick</option><option value=\"".$seriesRow["Team1"]."\">".$seriesRow["Team1"]."</option><option value=\"".$seriesRow["Team2"]."\">".$seriesRow["Team2"]."</option></select></td>
										<td align=\"center\">".makeDropDown($seriesRow["Week"],$seriesRow["Team1"],$UserPickedrow["NumGames"])."</td>";
								}
								$gamesOver = false;
							}
							
							if ($seriesRow["Winner"] != "-")
								echo "<td><B>".$seriesRow["Winner"]."</B></td><td width=\"30\"><img src=\"http://hockey.joshhughes.com/NHL/images/small/".$seriesRow["Winner"].".gif\"></td>";
							else
								echo "<td width=\"30\">&nbsp;</td><td>&nbsp;</td>";
							echo "<td>".$seriesRow["NumGames"]."</td></tr>";
								
						}//end if (($seriesRow["Team1"] != NULL) && ($seriesRow["Team1"] != ""))
					}while($seriesRow = mysql_fetch_array($result));
						
						// Print line of form buttons to submit the form if there are any games that can still be changed
						if (!$gamesOver)
						{
							echo "
							<tr>
								<td colspan=\"10\"><hr color=\"#cb0000\" size=\"1\"/></td>
							</tr>
							<tr>
							<td colspan=\"10\" align=\"center\">
							<input type=\"hidden\" name=\"UserID\" value=\"$userID\">
							<input type=\"reset\" value=\"Reset Form\"> | <input type=\"Submit\" name=\"Submit\" value=\"";
							if($update)
							echo "Update Picks\">";
							else
							echo "Submit Picks\">";
							echo "</td></tr>";
						}
						echo "</table></form>";
					?>
				</td></tr></table>
			<!-- End Main Contents -->
			
			</div>
			<div id="leftcol" >
				<?PHP include_once("layout/left_column.php"); ?>
			</div>
			
		</div>
		
		<div id="footer" >
			<?PHP include_once("layout/footer.html"); ?>
		</div>
</div>
</body>
</html>
<?
function makeDropDown($SeriesNum,$Team1,$UserPick)
{

	$html = "<select name=\"Series".$SeriesNum."Games\" class=\"select\" style=\"width: 35px;\"";
	if ($Team1 == "TBD")
		$html .= " disabled";
	$html .= ">";
	for ($x = 4; $x <=7; $x++)
	{
		if($x == $UserPick)
			$html .= "<option selected value=\"$x\">$x</option>";
		else
			$html .= "<option value=\"$x\">$x</option>";
	}
	
	$html .= "</select>";

	return $html;

}

?>